---
import BlogCard from "@/components/BlogCard.astro";
import Base from "@/layouts/Base.astro";
import { getSinglePage } from "@/lib/contentParser.astro";
import { getTaxonomy } from "@/lib/taxonomyParser.astro";
import { sortByDate } from "@/lib/utils/sortFunctions";
import taxonomyFilter from "@/lib/utils/taxonomyFilter";
import PageHeader from "@/partials/PageHeader.astro";

// get static paths for all categories
export async function getStaticPaths() {
  const BLOG_FOLDER = "blog";
  const categories = await getTaxonomy(BLOG_FOLDER, "categories");

  return categories.map((category) => {
    return {
      params: { category },
    };
  });
}

const { category } = Astro.params;

// get posts by category
const BLOG_FOLDER = "blog";
const posts = await getSinglePage(BLOG_FOLDER);
const filterByCategories = taxonomyFilter(posts, "categories", category!);
const sortedPosts = sortByDate(filterByCategories);
---

<Base title={category}>
  <PageHeader title={category} />
  <div class="section-sm pb-0">
    <div class="container">
      <div class="row">
        {
          sortedPosts.map((post) => (
            <div class="mb-14 md:col-6 lg:col-4">
              <BlogCard data={post} />
            </div>
          ))
        }
      </div>
    </div>
  </div>
</Base>
